package recursion;

/**
 * 递归实现冒泡排序
 *
 * @author yyz
 * @create 2023-11-12 13:38
 */
public class BubbleSort {
    public static void sort(int[] a) {
        f(a, a.length - 1);
    }

    /**
     * 冒泡排序递归函数
     *
     * @param a 待排序的数组
     * @param j 未排序的边界值
     */
    private static void f(int[] a, int j) {
        if (j == 0) {
            return;
        }
        int x = 0;
        for (int i = 0; i < j; i++) {
            if (a[i] > a[i + 1]) {
                int t = a[i];
                a[i] = a[i + 1];
                a[i + 1] = t;
                x = i;
            }
        }

        f(a, x);
    }
}
